package com.ta.gulimall.product.controller.redisson;

import org.redisson.api.RCountDownLatch;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CountDownLatchController {
    @Autowired
    RedissonClient redissonClient;

    @GetMapping("/trySetCount/{count}")
    public String trySetCount(@PathVariable("count") Long count) throws InterruptedException {
        RCountDownLatch countDownLatch = redissonClient.getCountDownLatch("countDownLatch");
        countDownLatch.trySetCount(count);

        return "设置成功投降人数" + count;
    }

    @GetMapping("/await")
    public String await() throws InterruptedException {
        RCountDownLatch countDownLatch = redissonClient.getCountDownLatch("countDownLatch");
        countDownLatch.await();

        return "投降成功";
    }

    @GetMapping("/countDown")
    public String countDown() throws InterruptedException {
        RCountDownLatch countDownLatch = redissonClient.getCountDownLatch("countDownLatch");
        countDownLatch.countDown();

        return "投票+1";
    }
}
